const importLazy = require('import-lazy')(require);
const inquirer = require('inquirer');
const buildModuleDescriptor = require('../okapi/buildModuleDescriptor');

const Okapi = importLazy('../okapi/okapiRepository.js');
const ModuleService = importLazy('../okapi/moduleService.js');

module.exports = async function registerModuleDescriptor(pakeage) {
  const answers = await inquirer.prompt([
    {
      type: 'input',
      name: 'okapiUrl',
      message: '请输入okapiUrl地址',
    },
  ]);
  const { okapiUrl } = answers;

  const okapi = new Okapi(okapiUrl);
  const moduleService = new ModuleService(okapi);
  const moduleDescriptor = buildModuleDescriptor(pakeage);

  return moduleService
    .addModuleDescriptor(moduleDescriptor)
    .then(() => {
      console.log(`register ${pakeage.name} ModuleDescriptor success!!!`);
    })
    .catch((err) => {
      console.log('Error registering in.', err);
    });
};
